<template>
  <div class="personal-main">
    <el-table :data="borrowInfos" stripe style="width: 100%">
      <el-table-column prop="amount" label="借款金额"></el-table-column>
      <el-table-column prop="period" label="期数"></el-table-column>
      <el-table-column prop="borrowYearRate" label="年化率"></el-table-column>
      <el-table-column prop="params.returnMethod" label="还款方式">
      </el-table-column>
      <el-table-column prop="params.moneyUse" label="资金用途">
      </el-table-column>
      <el-table-column prop="createTime" label="申请时间"></el-table-column>
      <el-table-column prop="params.status" label="状态"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              v-if="scope.row.status === 1"
              @click="handleCancel(scope.$index, scope.row)"
          >
            撤销申请
          </el-button
          >
          <el-tag size="mini">{{ scope.row.params.status }}</el-tag>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="pageNum"
        :page-size="pageSize"
        layout="total, prev, pager, next"
        :total="total"
    >
    </el-pagination>
  </div>
</template>
<script>
export default {
  data() {
    return {
      pageNum: 1,
      pageSize: 3,
      borrowInfos: [],
      total: 0,
    };
  },
  mounted() {
    this.initData();
  },
  methods: {
    handleSizeChange(size) {
      this.pageSize = size;
      this.initData();
    },
    handleCurrentChange(num) {
      this.pageNum = num;
      this.initData();
    },
    handleCancel(idx, row) {
      this.$confirm("此操作将撤销借款记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
          .then(() => {
            let token = localStorage.getItem("SRB-TOKEN");
            if (!token) {
              this.$message.warn("请先登录");
              return;
            }
            this.$axios({
              url: `/api/core/borrowInfo/my/cancel/${row.id}`,
              method: "PUT",
              headers: {token},
            }).then((r) => {
              this.$message.success(r.data.message);
              this.initData();
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消",
            });
          });
    },
    initData() {
      let token = localStorage.getItem("SRB-TOKEN");
      if (!token) {
        this.$message.warn("请先登录");
        return;
      }
      this.$axios({
        url: `/api/core/borrowInfo/my/borrows/${this.pageNum}/${this.pageSize}`,
        method: "GET",
        headers: {token},
      }).then((r) => {
        this.borrowInfos = r.data.data.page.records;
        this.total = Number(r.data.data.page.total);
      });
    },
  },
};
</script>
